<?php
/** 
 * @copyright Copyright (C) 2010 redCOMPONENT.com. All rights reserved. 
 * @license GNU/GPL, see license.txt or http://www.gnu.org/copyleft/gpl.html
 * Developed by email@recomponent.com - redCOMPONENT.com 
 *
 * redSHOP can be downloaded from www.redcomponent.com
 * redSHOP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with redSHOP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

defined ( '_JEXEC' ) or die ( 'Restricted access' );

JHTMLBehavior::modal();

jimport('joomla.filesystem.file');
require_once( JPATH_COMPONENT_SITE.DS.'helpers'.DS.'product.php' );
$uri =& JURI::getInstance();
$url= $uri->root();

$option = JRequest::getVar('option');
$producthelper = new producthelper();
$model = $this->getModel ( 'media' );

?>
<script language="javascript" type="text/javascript">

Joomla.submitbutton = function(pressbutton) {submitbutton(pressbutton);}
submitbutton = function(pressbutton){	
var form = document.adminForm;

   if (pressbutton)
    {form.task.value=pressbutton;}
     
	 if ((pressbutton=='add')||(pressbutton=='edit')||(pressbutton=='publish')||(pressbutton=='unpublish')
	 ||(pressbutton=='remove') || (pressbutton=='copy') || (pressbutton=='edit') || (pressbutton=='defaultmedia')|| (pressbutton=='saveorder')||(pressbutton=='orderup') ||(pressbutton=='orderdown'))
	 {		 
	  form.view.value="media_detail";
	 }
	try {
		form.onsubmit();
		}
	catch(e){}
	
	form.submit();
}

</script>
<?php

//--------- For Add Media Detail ---------------

$showbuttons=JRequest::getCmd('showbuttons');
$section_id = JRequest::getCmd('section_id');
$product_detail =$producthelper->getProductById($section_id);
$section_name = $product_detail->product_name;
//$section_name = JRequest::getCmd('section_name');
$media_section = JRequest::getCmd('media_section');

if($showbuttons==1)
{

?>
<fieldset>
	<div style="float: right">
		<button type="button" onclick="Joomla.submitbutton('add');">
			<?php echo JText::_('COM_REDSHOP_ADD' ); ?>
		</button>
		<button type="button" onclick="Joomla.submitbutton('edit');">
			<?php echo JText::_('COM_REDSHOP_EDIT' ); ?>
		</button>
		<button type="button" onclick="Joomla.submitbutton('defaultmedia');">
			<?php echo JText::_('COM_REDSHOP_DEFAULT_MEDIA' ); ?>
		</button>
		<button type="button" onclick="Joomla.submitbutton('remove');">
			<?php echo JText::_('COM_REDSHOP_DELETE' ); ?>
		</button>
		<button type="button" onclick="Joomla.submitbutton('publish');">
			<?php echo JText::_('COM_REDSHOP_PUBLISH' ); ?>
		</button>
		<button type="button" onclick="Joomla.submitbutton('unpublish');">
			<?php echo JText::_('COM_REDSHOP_UNPUBLISH' ); ?>
		</button>
		<button type="button" onclick="window.parent.location.reload();">
			<?php echo JText::_('COM_REDSHOP_CANCEL' ); ?>
		</button>
	</div>
	<div class="configuration"><?php echo JText::_('COM_REDSHOP_ADD_MEDIA' ); ?></div>
</fieldset>
<?php
	
	$action = 'index.php?tmpl=component&option=' . $option;

	//--------End-------------
	
}else{

	$action = 'index.php?option=' . $option;

}

?>
<form action="<?php echo $action;?>" method="post" name="adminForm" id="adminForm">
<div id="editcell">
<?php 
if($showbuttons !=1)
{?>
	<table class="adminlist">		 
		<tr>		
			<td valign="top" align="right" class="key">
			 <?php echo JText::_('COM_REDSHOP_MEDIA_TYPE' ); ?>:
				 
			<?php echo $this->lists['type']; ?>&nbsp;				
			<?php echo JText::_('COM_REDSHOP_MEDIA_SECTION' ); ?>:				 
			<?php echo $this->lists['section']; ?>&nbsp;
			<button onclick="this.form.getElementById('media_type').value='0';this.form.getElementById('media_section').value='0';this.form.submit();"><?php echo JText::_('COM_REDSHOP_RESET');?></button>
							
			</td>
		</tr>
	</table>
<?php }?>
<table class="adminlist">
	<thead>	
		<tr>
			<th width="5">
				<?php
				echo JText::_('COM_REDSHOP_NUM' );
				?>
			</th>
			<th width="20"><input type="checkbox" name="toggle" value=""
				onclick="checkAll(<?php
				echo count ( $this->media );
				?>);" /></th>
			<th class="title">
				<?php
				if($showbuttons==1)
					echo JText::_('COM_REDSHOP_MEDIA_NAME' );
				else
					echo JHTML::_ ( 'grid.sort', 'COM_REDSHOP_MEDIA_NAME', 'media_name', $this->lists ['order_Dir'], $this->lists ['order'] );
				?>
				
			</th>
			<th>
				<?php
				if($showbuttons==1)
					echo JText::_('COM_REDSHOP_MEDIA_TYPE');
				else
					echo JHTML::_ ( 'grid.sort', 'COM_REDSHOP_MEDIA_TYPE', 'media_type', $this->lists ['order_Dir'], $this->lists ['order'] );
				?>
		 	</th>
		 	<?php
				if($showbuttons==1){					
				?>
		 	<th>
				<?php
				
					echo JText::_('COM_REDSHOP_ADDITIONAL_DOWNLOAD_FILES');				
				?>
		 	</th>
		 	<?php }?>
		 	<th>
				<?php
				if($showbuttons==1)
					echo JText::_('COM_REDSHOP_MEDIA_ALTERNATE_TEXT');
				else
					echo JHTML::_ ( 'grid.sort', 'COM_REDSHOP_MEDIA_ALTERNATE_TEXT', 'media_alternate_text', $this->lists ['order_Dir'], $this->lists ['order'] );
				?>
		 	</th>
			<th >
				<?php
				if($showbuttons==1)
					echo JText::_('COM_REDSHOP_MEDIA_SECTION');
				else
					echo JHTML::_ ( 'grid.sort', 'COM_REDSHOP_MEDIA_SECTION', 'media_section', $this->lists ['order_Dir'], $this->lists ['order'] );
				?>
			</th>
			<?php
				if($showbuttons==1 && $media_section != 'catalog') {	?>
			<th class="title">
					<?php echo JText::_('COM_REDSHOP_PRIMARY_MEDIA' );?>
			</th>
			<?php }?>
            
           <!-- ordering-->
            <?php
				if($showbuttons==1 && $media_section == 'product') {	
				$mediadetail = $model->MediaDetail($section_id,$media_section);
					?>
			<th class="order" width="20%">				
				<?php  echo JHTML::_('grid.order',  $mediadetail ); ?>
			</th>
			<?php }?>
            
            
			<th width="5%" nowrap="nowrap">
				<?php
				if($showbuttons==1)
					echo JText::_('COM_REDSHOP_PUBLISHED');
				else
					echo JHTML::_ ( 'grid.sort', 'COM_REDSHOP_PUBLISHED', 'published', $this->lists ['order_Dir'], $this->lists ['order'] );
				?>	
			</th>
			<th width="5%" nowrap="nowrap">
				<?php
				if($showbuttons==1)
					echo JText::_('COM_REDSHOP_ID');
				else
					echo JHTML::_ ( 'grid.sort', 'COM_REDSHOP_ID', 'media_id', $this->lists ['order_Dir'], $this->lists ['order'] );
				?>	
			</th>

		</tr>
	</thead>
	
		<?php
		
		//------------------------------------------------------------------------
if($showbuttons==1){
	
	$mediadetail = $model->MediaDetail($section_id,$media_section);
	$primaryMedia = $model->getPrimaryMedia($section_id,$media_section);
	//echo '<pre/>'; print_r($this->media);exit;
	//echo '<pre/>'; print_r($mediadetail);exit;
	$k = 0;
	
	for($i = 0, $n = count($mediadetail); $i < $n; $i ++) 
	{
		$row1 = &$this->media[$i];
		$row = &$mediadetail[$i];
		$row->id = $row->media_id;
		$checked = "";
		if($media_section != 'catalog')
		{
			if(trim($primaryMedia->product_full_image)==trim($row->media_name))
			{
				$checked = "checked";
			}
		}
		$published = JHTML::_ ( 'grid.published', $row, $i );	?>
        
         <tr class="<?php
		echo "row$k";
		?>">
		<td>
				<?php
		echo $this->pagination->getRowOffset ( $i );
		?>
			</td>
		<td>
			<?php
		echo JHTML::_ ( 'grid.id', $i, $row->id );
		?>
			</td>
		<td width="30%">
		<?php 
		$filetype = strtolower(JFile::getExt(trim($row1->media_name)));
				$media_img=$url.'components/'.$option.'/assets/'.$row->media_type.'/'.$row->media_section.'/'.trim($row->media_name);
		if($filetype == 'png' || $filetype == 'jpg' || $filetype == 'jpeg' || $filetype == 'gif'){
		?>		
		<a class="modal" href="<?php echo $url.'components/'.$option.'/assets/'.$row->media_type.'/'.$row->media_section.'/'.trim($row->media_name);	?>"	title="<?php echo JText::_('COM_REDSHOP_VIEW_IMAGE' );?>"  rel="{handler: 'image', size: {}}"><img src="<?php echo $media_img?>" height="50" width="50"/></a>
		<?php 
		}else{
			echo $row->media_name;
		}
		?>
		</td>
		<td width="30%" class="order">
				<?php
		echo $row->media_type;
		
		?>
			</td>
		<td width="30%" class="order">
				<?php
					if($row->media_type == 'download'){	
					$additionalfiles = $model->getAdditionalFiles($row->id);							
				?>
					<a href="index.php?tmpl=component&option=com_redshop&view=media&layout=additionalfile&media_id=<?php echo $row->id;?>&showbuttons=1" class="modal" rel="{handler: 'iframe', size: {x: 1000, y: 400}}" title="<?php echo JText::_('COM_REDSHOP_ADDITIONAL_DOWNLOAD_FILES').'&nbsp;('.count($additionalfiles).')';?>" ><?php echo JText::_('COM_REDSHOP_ADDITIONAL_DOWNLOAD_FILES').'&nbsp;('.count($additionalfiles).')';?></a>				<?php }?>
			</td>	
		<td class="order"><?php	echo $row->media_alternate_text;?></td> 
		<td class="order" width="30%">
				<?php
		echo $row->media_section;
		?>
				
			</td>
			<?php if($media_section != 'catalog'){ ?>
		<td align="center" width="5%">
		<input type="radio" name="primary" id="<?php echo $row->media_name;?>" value="<?php echo $row->media_name;?>" <?php echo $checked;?> /></td>
        <?php } ?>
        
        <!--ordering-->
        <td align="center" width="5%">
        <?php  echo $this->pagination->orderUpIcon( $i, true, 'orderup', 'Move Up', $row->ordering );  
				echo $this->pagination->orderDownIcon( $i, $n, true, 'orderdown', 'Move Down', $row->ordering );?>
				<input type="text" name="order[]" size="5" value="<?php echo $row->ordering;?>" class="text_area" style="text-align: center" /></td>
               
        
        
        
		<td align="center" width="5%">
				<?php
		echo $published;
		?>
			</td>
		<td align="center" width="5%">
				<?php
		echo $row->media_id;
		?>
			</td>
	</tr>
		<?php
		$k = 1 - $k;
	}
	?>
	<input type="hidden" name="showbuttons" value="<?php echo $showbuttons;?>" />
	<input type="hidden" name="section_id" value="<?php echo $section_id;?>" />
	<input type="hidden" name="media_section" value="<?php echo $media_section;?>" />
	<input type="hidden" name="section_name" value="<?php echo $section_name;?>" />
    
	<?php 
}else{
	//------------------------------------
	
	$k = 0;
	for($i = 0, $n = count ( $this->media ); $i < $n; $i ++) {
		$row = &$this->media [$i];
		$row->id = $row->media_id;
		//if($row->media_section == 'product')
			
		$published = JHTML::_ ( 'grid.published', $row, $i );
		
		?>
		<tr class="<?php
		echo "row$k";
		?>">
		<td>
				<?php
		echo $this->pagination->getRowOffset ( $i );
		?>
			</td>
		<td>
			<?php
		echo JHTML::_ ( 'grid.id', $i, $row->id );
		?>
			</td>
		<td width="30%">
		<?php 
		$filetype = strtolower(JFile::getExt($row->media_name));
		$media_img=$url.'components/'.$option.'/assets/'.$row->media_type.'/'.$row->media_section.'/'.$row->media_name;
		if($filetype == 'png' || $filetype == 'jpg' || $filetype == 'jpeg' || $filetype == 'gif')
		{
			
			?>		
			<a class="modal" href="<?php echo $url.'components/'.$option.'/assets/'.$row->media_type.'/'.$row->media_section.'/'.$row->media_name;	?>"	title="<?php echo JText::_('COM_REDSHOP_VIEW_IMAGE' );?>"  rel="{handler: 'image', size: {}}"> <img src="<?php echo $media_img?>" height="50" width="50"/></a>
			<?php 
			
			
		}
		else{
			echo $row->media_name;
		}
		?>
		</td>
		<td width="30%" class="order">
				<?php
		echo $row->media_type;
		
		?>
			</td><td class="order">
				<?php
		echo $row->media_alternate_text;
		
		?>
			</td>
		<td class="order" width="30%">
				<?php
		echo $row->media_section;
		?>
				
			</td>
		<td align="center" width="5%">
				<?php
		echo $published;
		?>
			</td>
		<td align="center" width="5%">
				<?php
		echo $row->media_id;
		?>
			</td>
	</tr>
		<?php
		$k = 1 - $k;
	}
	
	?>	

<tfoot>
		<td colspan="9">
			<?php
			echo $this->pagination->getListFooter ();
			?>
		</td>
</tfoot>
<?php }?>
</table>
</div>

<input type="hidden" name="view" value="media" /> 
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" /> 
<input type="hidden" name="filter_order" value="<?php echo $this->lists ['order'];?>" />
<input type="hidden" name="filter_order_Dir" value="<?php echo $this->lists ['order_Dir'];?>" />

</form>
